package com.dreamwhale.machinery.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.config.StringUtils;
import com.dreamwhale.machinery.dao.*;
import com.dreamwhale.machinery.entity.*;
import com.dreamwhale.machinery.goods.mapper.YxStoreProductMapper;
import com.dreamwhale.machinery.goods.entity.*;
import com.dreamwhale.machinery.goods.mapper.ProductBrandMapper;
import com.dreamwhale.machinery.goods.mapper.ProductModelMapper;
import com.dreamwhale.machinery.goods.mapper.YxStoreCategoryMapper;
import com.dreamwhale.machinery.goods.service.*;
import com.dreamwhale.machinery.logistics.entity.TransportCompanyInfo;
import com.dreamwhale.machinery.logistics.entity.TransportInfo;
import com.dreamwhale.machinery.logistics.service.TransportCompanyInfoService;
import com.dreamwhale.machinery.logistics.service.TransportInfoService;
import com.dreamwhale.machinery.service.LeaseService;
import com.dreamwhale.machinery.service.YxStoreProductService;
import com.dreamwhale.machinery.user.entity.UserShop;
import com.dreamwhale.machinery.user.mapper.UserShopMapper;
import com.dreamwhale.machinery.user.service.UserShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 二手机械购买 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@RestController
@RequestMapping("/user")
public class MachineBuyController {

    @Resource
    private ProductBrandMapper productBrandMapper;

    @Resource
    private ProductModelMapper productModelMapper;

    @Resource
    private MachineSellService machineSellService;

    @Resource
    private DriverInfoService driverInfoService;

    @Resource
    private DriverWorkInfoService driverWorkInfoService;

    @Resource
    private MachineBuyService machineBuyService;

    @Resource
    private TransportCompanyInfoService transportCompanyInfoService;

    @Resource
    private TransportInfoService transportInfoService;

    @Resource
    private UserShopMapper userShopMapper;

    @Resource
    private UserShopService userShopService;

    @Resource
    private YxStoreCategoryMapper yxStoreCategoryMapper;

    @Resource
    private YxStoreProductMapper yxStoreProductMapper;

    @Resource
    private YxStoreProductService yxStoreProductService;

    @Resource
    private PriceRangeMapper priceRangeMapper;

    @Resource
    private AboutUsMapper aboutUsMapper;

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private UserShopCollectMapper userShopCollectMapper;

    @Resource
    private ProjectSeekService projectSeekService;

    @Resource
    private LeaseService leaseService;

    @Resource
    private DriveTypesMapper driveTypesMapper;

    /**
     * 查询品牌
     *
     * @return
     */
    @RequestMapping("/selectProductBrands")
    public DataResult selectProductBrands() {
        QueryWrapper<ProductBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        return DataResult.ok(productBrandMapper.selectList(queryWrapper));
    }

    /**
     * 查询司机类型
     *
     * @return
     */
    @RequestMapping("/selectDriveTypes")
    public DataResult selectDriveTypes() {
        QueryWrapper<DriveTypes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        return DataResult.ok(driveTypesMapper.selectList(queryWrapper));
    }


    /**
     * 查询价格区间
     *
     * @return
     */
    @RequestMapping("/selectPriceRangs")
    public DataResult selectPriceRangs() {
        QueryWrapper<PriceRange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        return DataResult.ok(priceRangeMapper.selectList(queryWrapper));
    }


    /**
     * 查询型号
     *
     * @param brandId
     * @return
     */
    @RequestMapping("/selectProductModels")
    public DataResult selectProductModels(@Param("brandId") Integer brandId) {
        QueryWrapper<ProductModel> queryWrapper = new QueryWrapper<>();
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }
        return DataResult.ok(productModelMapper.selectList(queryWrapper));
    }

    /**
     * 发布二手机械
     *
     * @param machineSell
     * @return
     */
    @RequestMapping("/insertMachineSells")
    public DataResult selectProductModels(MachineSell machineSell) {
        int count = machineSellService.insertMachineSells(machineSell);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }

    /**
     * 发布租赁
     *
     * @param lease
     * @return
     */
    @RequestMapping("/insertLeases")
    public DataResult insertLeases(Lease lease) {
        int count = leaseService.insertLeases(lease);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }

    /**
     * 发布找驾驶员
     *
     * @param driverInfo
     * @return
     */
    @RequestMapping("/insertDriverInfos")
    public DataResult insertDriverInfos(DriverInfo driverInfo) {
        int count = driverInfoService.insertDriverInfos(driverInfo);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }


    /**
     * 发布找工作
     *
     * @param driverWorkInfo
     * @return
     */
    @RequestMapping("/insertDriverWorkInfos")
    public DataResult insertDriverWorkInfos(DriverWorkInfo driverWorkInfo) {
        int count = driverWorkInfoService.insertDriverWorkInfos(driverWorkInfo);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }


    /**
     * 发布找机械
     *
     * @param machineBuy
     * @return
     */
    @RequestMapping("/insertMachineBuys")
    public DataResult insertMachineBuys(MachineBuy machineBuy) {


        int count = machineBuyService.insertMachineBuys(machineBuy);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }

    /**
     * 发布物流信息
     *
     * @param transportCompanyInfo
     * @return
     */
    @RequestMapping("/insertTransportCompanyInfos")
    public DataResult insertTransportCompanyInfos(TransportCompanyInfo transportCompanyInfo) {
        int count = transportCompanyInfoService.insertTransportCompanyInfos(transportCompanyInfo);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }

    /**
     * 发布找工程
     *
     * @param projectSeek
     * @return
     */
    @RequestMapping("/insertProjectSeeks")
    public DataResult insertProjectSeeks(ProjectSeek projectSeek) {
        int count = projectSeekService.insertProjectSeeks(projectSeek);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }

    /**
     * 发布托运信息
     *
     * @param transportInfo
     * @return
     */
    @RequestMapping("/insertTransportInfos")
    public ResponseEntity<DataResult> insertTransportInfos(TransportInfo transportInfo) {
        int count = transportInfoService.insertTransportInfos(transportInfo);
        if (count > 0) {
            return ResponseEntity.ok(DataResult.ok(1));
        }
        return ResponseEntity.ok(DataResult.build(300, "添加失败"));
    }


    /**
     * 查询商户入驻
     *
     * @return
     */
    @RequestMapping("/selectUserShops")
    public DataResult selectUserShops() {
        QueryWrapper<UserShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1).eq("is_del", 0).orderByDesc("add_time");
        queryWrapper.select("merchant_name");
        return DataResult.ok(userShopMapper.selectList(queryWrapper));
    }

    /**
     * 查询一级分类
     *
     * @return
     */
    @RequestMapping("/selectYxStoreCategorys")
    public DataResult selectYxStoreCategorys() {
        QueryWrapper<YxStoreCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0).orderByDesc("add_time");
        return DataResult.ok(yxStoreCategoryMapper.selectList(queryWrapper));
    }


    /**
     * 根据一级分类查询二级分类
     *
     * @return
     */

    @RequestMapping("/selectYxStoreCategorysTwo")
    public DataResult selectYxStoreCategorysTwo() {

        QueryWrapper<YxStoreCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0).eq("is_del", 0).orderByAsc("id");

        List<Map> maps = new ArrayList<Map>();
        List<YxStoreCategory> yxStoreCategories = yxStoreCategoryMapper.selectList(queryWrapper);
        yxStoreCategories.forEach(v -> {
            HashMap<String, Object> map = new HashMap<>();
            Integer pid = v.getId();
            QueryWrapper<YxStoreCategory> yxStoreCategoryQueryWrapper = new QueryWrapper<>();
            yxStoreCategoryQueryWrapper.eq("pid", pid).eq("is_del", 0).orderByAsc("id");
            List<YxStoreCategory> yxStoreCategories1 = yxStoreCategoryMapper.selectList(yxStoreCategoryQueryWrapper);
            map.put("name", v.getCateName());
            map.put("data", yxStoreCategories1);
            maps.add(map);
        });
        return DataResult.ok(maps);
    }


    /**
     * 查询配件分类
     *
     * @param type      销量  1倒叙 2正序
     * @param types     价格  1倒叙 2正序
     * @param brandId   品牌id
     * @param cateId    分类id
     * @param priceId   价格区间id
     * @param storeName 商品名称
     * @return
     */
    @RequestMapping("/selectYxStoreProductSaleAsc")
    public DataResult selectYxStoreProductSaleAsc(@Param("type") Integer type, @Param("types") Integer types, Integer brandId, Integer cateId,
                                                  Integer priceId, String storeName, Integer page, Integer limit) {

        PageHelper.startPage(page, limit, true);
        List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductSaleAsc(type, types, brandId, cateId, priceId, storeName);

        //查询商户昵称

        for (YxStoreProduct yxStoreProduct : list) {
            QueryWrapper<UserShop> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", yxStoreProduct.getMerId());
            List<UserShop> lists = userShopMapper.selectList(queryWrapper);
            if (StringUtils.isNotEmpty(lists) && yxStoreProduct.getMerId() != 0) {
                yxStoreProduct.setMerchantName(lists.get(0).getMerchantName());
            }

            QueryWrapper<ProductBrand> listb = new QueryWrapper<>();
            listb.eq("id", yxStoreProduct.getBrandId());
            ProductBrand brand = productBrandMapper.selectOne(listb);
            yxStoreProduct.setBrandName(yxStoreProduct.getBrandId() == null ? null : brand.getName());

        }

        PageInfo<YxStoreProduct> pages = new PageInfo<>(list);
        return DataResult.ok(pages);
    }


    /**
     * 根据商品id查询详情
     *
     * @return
     */
    @RequestMapping("/selectUserShopMassage")
    public DataResult selectUserShopMassage(String storeProductId) {

        HashMap<String, Object> map = new HashMap<>();

        YxStoreProduct yxStoreProduct = yxStoreProductService.selectYxStoreProductById(storeProductId);
        map.put("yxStoreProduct", yxStoreProduct);

        UserShop userShop = userShopService.selectUserShopMassage(yxStoreProduct.getMerId());
        map.put("userShop", userShop);

        List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductCount(yxStoreProduct.getMerId());
        map.put("list", list.size());

        return DataResult.ok(map);
    }


    /**
     * 查看我的设备
     *
     * @return
     */
    @RequestMapping("/selectMyEquipment")
    public DataResult selectMyEquipment() {

        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_share", 1).eq("is_show", 1);
        YxStoreProduct yxStoreProduct = yxStoreProductService.getOne(queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        if (yxStoreProduct != null) {
            //商品
            map.put("yxStoreProduct", yxStoreProduct);
            //店铺
            UserShop userShop = userShopService.selectUserShopMassage(yxStoreProduct.getMerId());
            map.put("userShop", userShop);
            //店铺再卖商品数量
            List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductCount(yxStoreProduct.getMerId());
            map.put("list", list.size());
            return DataResult.ok(map);
        } else {
            return DataResult.ok();
        }

    }

    /**
     * 商家入驻
     *
     * @param userShop
     * @return
     */
    @RequestMapping("/insertUserShops")
    public DataResult insertUserShops(UserShop userShop) {
        int count = userShopService.insertUserShops(userShop);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "入驻失败");
    }

    /**
     * 查询关于我们
     *
     * @return
     */
    @RequestMapping("/selectAboutUsMassage")
    public DataResult selectAboutUsMassage() {
        QueryWrapper<AboutUs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("about_del", 0);
        return DataResult.ok(aboutUsMapper.selectList(queryWrapper));
    }

    /**
     * 查询客服
     *
     * @return
     */
    @RequestMapping("/selectCustomerMassage")
    public DataResult selectCustomerMassage() {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_del", 0);
        return DataResult.ok(customerMapper.selectList(queryWrapper));
    }


    /**
     * 根据商户id查询商品
     *
     * @param type    销量  1倒叙 2正序
     * @param types   价格  1倒叙 2正序
     * @param brandId 品牌id
     * @param cateId  分类id
     * @param merId   店铺id
     * @return
     */
    @RequestMapping("/selectYxStoreProductByMerId")
    public DataResult selectYxStoreProductByMerId(@Param("type") Integer type, @Param("types") Integer types, Integer brandId, Integer cateId,
                                                  Integer merId, Integer page, Integer limit, String userId) {

        PageHelper.startPage(page, limit, true);
        List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductByMerId(merId, type, types, cateId, brandId);

        //查询商户昵称
        for (YxStoreProduct yxStoreProduct : list) {
            QueryWrapper<UserShop> queryWrapper = new QueryWrapper<>();
            QueryWrapper<ProductBrand> listb = new QueryWrapper<>();
            listb.eq("id", yxStoreProduct.getBrandId());
            ProductBrand brand = productBrandMapper.selectOne(listb);
            yxStoreProduct.setBrandName(yxStoreProduct.getBrandId() == null ? null : brand.getName());
            QueryWrapper<UserShopCollect> queryWrappe = new QueryWrapper<>();
            queryWrappe.eq("uid", userId).eq("shop_id", merId);
            List<UserShopCollect> listc = userShopCollectMapper.selectList(queryWrappe);
            if (listc.size() > 0) {
                yxStoreProduct.setCollect(1);
            }

        }

        PageInfo<YxStoreProduct> pages = new PageInfo<>(list);
        return DataResult.ok(pages);
    }


    /**
     * 查询热门推荐
     *
     * @return
     */
    @RequestMapping("/selectYxStoreProductHot")
    public DataResult selectYxStoreProductHot() {

        List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductHot();
        //查询品牌
        for (YxStoreProduct yxStoreProduct : list) {
            QueryWrapper<ProductBrand> listb = new QueryWrapper<>();
            listb.eq("id", yxStoreProduct.getBrandId());
            ProductBrand brand = productBrandMapper.selectOne(listb);
            yxStoreProduct.setBrandName(yxStoreProduct.getBrandId() == null ? null : brand.getName());

        }

        return DataResult.ok(list);
    }

    /**
     * 查询特价优惠
     *
     * @return
     */
    @RequestMapping("/selectYxStoreProductBenefit")
    public DataResult selectYxStoreProductBenefit() {

        List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductBenefit();
        //查询商户昵称
        for (YxStoreProduct yxStoreProduct : list) {
            QueryWrapper<UserShop> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", yxStoreProduct.getMerId());
            List<UserShop> lists = userShopMapper.selectList(queryWrapper);
            if (yxStoreProduct.getMerId() != 0) {
                yxStoreProduct.setMerchantName(lists.get(0).getMerchantName());
            }

            QueryWrapper<ProductBrand> listb = new QueryWrapper<>();
            listb.eq("id", yxStoreProduct.getBrandId());
            ProductBrand brand = productBrandMapper.selectOne(listb);
            yxStoreProduct.setBrandName(yxStoreProduct.getBrandId() == null ? null : brand.getName());
        }

        return DataResult.ok(list);
    }


    /**
     * 店铺收藏
     *
     * @param userShopCollect
     * @return
     */
    @RequestMapping("/insertUserShopsCollect")
    public DataResult insertUserShopsCollect(UserShopCollect userShopCollect) {
        userShopCollect.setCreateTime(LocalDateTime.now());
        int count = userShopCollectMapper.insert(userShopCollect);
        if (count > 0) {
            return DataResult.ok(1);
        }
        return DataResult.build(300, "添加失败");
    }
}